<template>
  <Header></Header>
  <Drawer></Drawer>
  <el-breadcrumb separator="/" style="font-size: 20px;color: #2AB28B;">
    <el-breadcrumb-item :to="{ path: '/Home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item :to="{ path: '/Home' }">{{ labName }}</el-breadcrumb-item>
    <el-breadcrumb-item :to="{ path: '/pageList/' + groupId }">档案管理</el-breadcrumb-item>
    <el-breadcrumb-item>膳食分析</el-breadcrumb-item>
  </el-breadcrumb>

  <div class='pageListBotton'>
    <el-button :icon="Expand" style="color: white;background: #2AB28B" round @click="pageList">
      档案列表
    </el-button>
    <span class="onBtnRight" @click="onHome"><img src="../assets/img/pageList_qiehuan.png" alt=""> 更换群体</span>
  </div>

  <ChildLeft></ChildLeft>

  <el-card class="right-box-card">
    <el-scrollbar style="height:100%;width: 100%"> <!-- 滚动条 -->
      <div class="">
        <div class="">
          <div class="">
            <div class="">
              <div class="">
                <div
                  style="margin: 20px 0px;width: 60%;height:100vh;position:fixed;top:10%;background-color: #fff;left:20%;box-shadow: var(--el-box-shadow-light);border-radius: var(--el-card-border-radius);z-index: 999;"
                  :class="default_1 == false ? 'types1' : 'types2'">
                  <div style="width: 100%;height: 50px;margin-top: 10px;text-align: right;" @click="closes">
                    <img style="width: 50px;height: 50px;margin-right: 10px;" src="../../public/staic/img/close.png" alt="">
                  </div>
                  <div style="display:flex;width: 70%;margin:50px auto;">
                    <el-select v-model="value1" class="m-2" placeholder="请选择" size="large" style="width: 100px">
                      <el-option v-for="item in options1" :key="item.value" :label="item.label" :value="item.value"
                        @click="option1(item)" />
                    </el-select>
                    <el-select v-model="value2" class="m-2" placeholder="请选择" size="large"
                      style="width: 100px;margin-left:10px;">
                      <el-option v-for="item in options2" :key="item.value" :label="item.label" :value="item.value"
                        @click="option2(item)" />
                    </el-select>
                    <el-select v-model="value3" class="m-2" placeholder="请选择" size="large"
                      style="width: 150px;margin-left:10px;">
                      <el-option v-for="item in options3" :key="item.value" :label="item.label" :value="item.value"
                        @click="option3(item)" />
                    </el-select>
                    <el-select v-model="value4" class="m-2" placeholder="请选择" size="large"
                      style="width: 100px;margin-left:10px;">
                      <el-option v-for="item in options4" :key="item.value" :label="item.label" :value="item.value"
                        @click="option4(item)" />
                    </el-select>
                    <el-select v-model="value5" class="m-2" placeholder="请选择" size="large"
                      style="width: 150px;margin-left:10px;">
                      <el-option v-for="item in options5" :key="item.value" :label="item.label" :value="item.value"
                        @click="option5(item)" />
                    </el-select>
                  </div>
                  <div style="width:70%;margin:auto;display:flex;">
                    <el-input v-model="foodname" style="width:50%;margin-left:10px;" placeholder="请输入菜名" />
                    <div
                      style="margin-left:10px;width: 70px;height:40px;background-color:#2AB28B;color:#fff;font-size:14px;text-align:center;line-height:40px;border-radius:5px;cursor: pointer;"
                      @click="searss">搜索</div>
                    <div
                      style="margin-left:10px;width: 70px;height:40px;background-color:#2AB28B;color:#fff;font-size:14px;text-align:center;line-height:40px;border-radius:5px;cursor: pointer;"
                      @click="closes">关闭</div>
                  </div>
                  <el-table :data="tableData" border style="width:80%;margin:20px auto;text-align:center;">
                    <el-table-column prop="food_name" label="名称" width="180" />
                    <el-table-column prop="energy" label="能量(kcal)" width="100" />
                    <el-table-column prop="weight" label="重量(g)" width="100" />
                    <el-table-column prop="carbohydrate" label="碳水化合物(g)" width="120" />
                    <el-table-column prop="fat" label="脂肪(g)" width="100" />
                    <el-table-column prop="protein" label="蛋白质(g)" width="100" />
                    <el-table-column label="操作">
                      <template #default="scope">
                        <div
                          style="width: 50%;background-color:#2AB28B;color:#fff;text-align:center;cursor: pointer;border-radius:5px;"
                          @click="handleEdit(scope.$index, scope.row)">追加</div>
                      </template>
                    </el-table-column>
                  </el-table>
                  <div style="width:50%;margin:auto;text-align:center;margin-top:20px;">
                    <el-pagination background layout="prev, pager, next" :total="pageTotal" @prev-click="previousPage"
                      @next-click="nextPage" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
                  </div>
                </div>
              </div>
              <div style="display: flex;">
                <div
                style="margin-left:2%;margin-bottom:20px;background-color:#2AB28B;width: 100px;height:40px;border-radius:5px;text-align:center;line-height:40px;color:#fff;font-size:14px;cursor: pointer;"
                @click="adds">追加菜品 </div>
                <div
                :style="pushstatus==2?'margin-left:2%;margin-bottom:20px;background-color:#f56c6c;padding:0px 10px;height:40px;border-radius:5px;text-align:center;line-height:40px;color:#fff;font-size:14px;cursor: pointer;':'margin-left:2%;padding:0px 10px;margin-bottom:20px;background-color:#2AB28B;height:40px;border-radius:5px;text-align:center;line-height:40px;color:#fff;font-size:14px;cursor: pointer;'"
                @click="opens1">{{pushstatus==1?'推送开启中。。。':'推送关闭中。。。'}} </div>
              </div>
              <div class=""
                style="width:100%;display: flex;text-align: center;padding: 20px 0px;background-color: #f5f7fa;">
                <div style="width:7%;">编号</div>
                <!-- <div>系统编号</div> -->
                <div style="width:11%;margin-left: 3%;">能量值（kcal）</div>
                <div style="width:20%;margin-left: 3%;">名称</div>
                <div style="width:10%;margin-left: 3%;">重量（g）</div>
                <div style="width:10%">份数</div>
                <div style="width:15%;margin-left: 3%;">类别</div>
                <div style="width:10%;">同菜替换</div>
                <div style="width:10%;margin-left: 3%;">操作</div>
                <!-- <div style="margin-left:10%">餐品类型</div> -->
              </div>
              <el-row style="height: 400px;width: 100%;z-index: 99;"><!--可显示区域-->
                <el-scrollbar max-height="400px" style="width:100%">
                  <div class="pg_hs" v-for="(item, index) in saveArry" :key="index"
                    style="display:flex;width: 100%;text-align: center;">
                    <div style="margin-top:60px;width:7%;">{{ index + 1 }}</div>
                    <!-- <div style="margin-left:2%;margin-top:30px">{{item.id}}</div> -->
                    <div style="margin-top:60px;width:10%;margin-left: 3%;">{{ item.energy }}</div>
                    <div style="overflow: hidden;margin-top:60px;width:20%;margin-left: 2%;height: 24px;">
                      {{ item.food_name }}</div>
                    <div style="margin-top:60px;width:10%;">{{ item.weight }}</div>
                    <div style="padding-top:50px;">
                      <template v-if="!loadings">
                        <el-input-number v-model="item.num" @change="onChange(index, item.num)" :min="0.1" :step="0.1"
                          style="width:150px;"></el-input-number>
                      </template>
                    </div>
                    <el-select v-model="item.single.checkList" class="m-2" placeholder="早餐" size="large"
                      style="width: 100px;margin-top:50px;margin-left: 50px;">
                      <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
                    </el-select>
                    <el-select v-model="item.value" class="m-2" :placeholder="item.food_name" size="large"
                      style="width: 150px;margin-top:50px;margin-left: 3%;">
                      <el-option v-for="(items, indexs) in item.single.replace" @click="replaces(items, index)"
                        :key="items.food_name" :label="items.food_name" :value="items.food_name" />
                    </el-select>
                    <div
                      style="width: 40px;height:30px;margin-top:55px;margin-left: 20px;background-color:#2AB28B;font-size:14px;color:#fff;text-align:center;line-height:30px;cursor: pointer;border-radius:5px;padding:0px 5px;"
                      @click="deletes(index)">
                      删除
                    </div>
                  </div>
                </el-scrollbar>
              </el-row>
            </div>
          </div>
        </div>
      </div>
      <div class="all">
        <!-- <span style="margin-right: 5px;font-size:14px">油能量</span>
      <span style="margin-right: 35px;font-size:14px">{{ OilEnergy }}（kcal）</span> -->
        <span>总计：{{ list.length ? list.length : 0 }}(个)</span>
        总能量：
        <input disabled="disabled" class="num1" type="text" style="width:100px" v-model="energys" />
        <span style="font-size: 16px;">（kcal）</span>
        <button @click="btn()" style="border: #f56c6c;background: #f56c6c;cursor: pointer;">查看报告</button>
        <button @click="type_3 == 'true' ? drawer = true : drawer = false"
          :style="type_3 == 'true' ? 'border: #e6a23c;background: #e6a23c;width: 120px;cursor: pointer;' : 'border: #a1a8a8;background: #a1a8a8;width: 120px;'">体成分分析</button>
        <button @click="btn_fenxi()" style="border: #67c23a;background: #67c23a;cursor: pointer;">膳食报告</button>
        <button @click="get_new()" style="border: #409eff;background: #409eff;cursor: pointer;">重新获取</button>
      </div>
    </el-scrollbar>

    <div :class="{ 'tan': tan == true, 'la': tan == true, 'pi': tan == false }">
      <div class="inner">
        <!-- <div class="tu">
          <img src="@/images/icon-area.svg" alt />
        </div>-->
        <!-- <div class="cg">分析完成</div> -->

        <el-drawer v-model="drawer" title="请选择要操作的内容">
          <!-- <div class="drVal">
            <el-tooltip
                class="box-item"
                effect="dark"
                content="点击此按钮手动输入体成分数据，可以无缝衔接体成分资料"
                placement="bottom"
                label="ttb"
            >
              <el-button class="btnClick" @click="handClick">手动输入体成分数据</el-button>
            </el-tooltip>

          </div> -->
          <div class="drVal">
            <el-tooltip class="box-item" effect="dark" content="点击此按钮进行体成分分析，如果没有反应请多试几次或者联系管理员哦" placement="bottom">
              <el-button type="primary" class="btnClick" @click="appraisal">开始体成分测评</el-button>
            </el-tooltip>

          </div>
          <div class="drVal">
            <el-tooltip class="box-item" effect="dark" content="用户测评完成之后，要点击此处，如点击测评无法显示，请先点击测评完毕" placement="bottom">
              <el-button type="info" class="btnClick" @click="guanbi">测评完毕</el-button>
            </el-tooltip>

          </div>
          <div class="drVal">
            <el-tooltip class="box-item" effect="dark" content="点击此按钮可以直接查看报告，此报告为体成分分析仪分析报告，请先进行体成分分析检测"
              placement="bottom">
              <el-button type="success" class="btnClick" @click="goto">查看体成分报告</el-button>
            </el-tooltip>
          </div>
          <div class="drVal">
            <el-tooltip class="box-item" effect="dark" content="膳食模型放好之后不要移动，确认放好之后点击此按钮，否则不会显示最新膳食哦" placement="bottom">
              <el-button type="warning" class="btnClick" @click="btn()">查看报告</el-button>
            </el-tooltip>
          </div>
        </el-drawer>

      </div>
    </div>
  </el-card>

  <el-drawer v-model="popWrite" :direction="popWriteDirection" :size="size">
    <template #title>
      <h4>请输入体成分对应数据</h4>
    </template>
    <template #default>
      <el-form :model="popWrite" label-width="220px" :inline="true" class="demo-form-inline">

        <div>
          <el-form-item label="* 脂肪">
            <el-input v-model="formInline.Fat" placeholder="脂肪参数必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 脂肪最小值">
            <el-input v-model="formInline.FatMin" placeholder="脂肪最小值必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 脂肪最大值">
            <el-input v-model="formInline.FatMax" placeholder="脂肪最大值必填数字"></el-input>
          </el-form-item>
        </div>
        <div>
          <el-form-item label="* 骨质">
            <el-input v-model="formInline.Bone" placeholder="骨质参数必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 骨质最小值">
            <el-input v-model="formInline.BoneMin" placeholder="骨质最小值必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 骨质最大值">
            <el-input v-model="formInline.BoneMax" placeholder="骨质最大值必填数字"></el-input>
          </el-form-item>
        </div>
        <div>
          <el-form-item label="* 蛋白质">
            <el-input v-model="formInline.Protein" placeholder="蛋白质参数必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 蛋白质最小值">
            <el-input v-model="formInline.ProteinMin" placeholder="蛋白质最小值必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 蛋白质最大值">
            <el-input v-model="formInline.ProteinMax" placeholder="蛋白质最大值必填数字"></el-input>
          </el-form-item>
        </div>
        <div>
          <el-form-item label="* 水分">
            <el-input v-model="formInline.Water" placeholder="水分参数必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 水分最小值">
            <el-input v-model="formInline.WaterMin" placeholder="水分最小值必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 水分最大值">
            <el-input v-model="formInline.WaterMax" placeholder="水分最大值必填数字"></el-input>
          </el-form-item>
        </div>
        <div>
          <el-form-item label="* 去脂体重">
            <el-input v-model="formInline.LBM" placeholder="去脂体重参数必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 细胞内液最小值">
            <el-input v-model="formInline.ICW" placeholder="细胞内液最小值必填数字"></el-input>
          </el-form-item>
        </div>
        <div>
          <el-form-item label="* 体重">
            <el-input v-model="formInline.Weight" placeholder="体重参数必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 体重最小值">
            <el-input v-model="formInline.WeightMin" placeholder="体重最小值必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 体重最大值">
            <el-input v-model="formInline.WeightMax" placeholder="体重最大值必填数字"></el-input>
          </el-form-item>
        </div>
        <div>
          <el-form-item label="* 肌肉">
            <el-input v-model="formInline.Muscle" placeholder="肌肉参数必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 肌肉最小值">
            <el-input v-model="formInline.MuscleMin" placeholder="肌肉最小值必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 肌肉最大值">
            <el-input v-model="formInline.MuscleMax" placeholder="肌肉最大值必填数字"></el-input>
          </el-form-item>
        </div>
        <div>
          <el-form-item label="* 体脂百分比">
            <el-input v-model="formInline.PBF" placeholder="体脂百分比参数必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 体脂百分比最小值">
            <el-input v-model="formInline.PBFMin" placeholder="体脂百分比最小值必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 体脂百分比最大值">
            <el-input v-model="formInline.PBFMax" placeholder="体脂百分比最大值必填数字"></el-input>
          </el-form-item>
        </div>
        <div>
          <el-form-item label="* 骨骼肌">
            <el-input v-model="formInline.SMM" placeholder="骨骼肌参数必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 骨骼肌最小值">
            <el-input v-model="formInline.SMMMin" placeholder="骨骼肌最小值必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 骨骼肌最大值">
            <el-input v-model="formInline.SMMMax" placeholder="骨骼肌最大值必填数字"></el-input>
          </el-form-item>
        </div>
        <div>
          <el-form-item label="* 体质指数">
            <el-input v-model="formInline.BMI" placeholder="体质指数参数必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 体质指数最小值">
            <el-input v-model="formInline.BMIMin" placeholder="体质指数最小值必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 体质指数最大值">
            <el-input v-model="formInline.BMIMax" placeholder="体质指数最大值必填数字"></el-input>
          </el-form-item>
        </div>
        <div>
          <el-form-item label="* 腰臀比">
            <el-input v-model="formInline.WHR" placeholder="腰臀比参数必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 腰臀比最小值">
            <el-input v-model="formInline.WHRMin" placeholder="腰臀比最小值必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 腰臀比最大值">
            <el-input v-model="formInline.WHRMax" placeholder="腰臀比最大值必填数字"></el-input>
          </el-form-item>
        </div>
        <div>
          <el-form-item label="* 体重控制">
            <el-input v-model="formInline.Weight_control" placeholder="体重控制参数必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 脂肪控制量">
            <el-input v-model="formInline.Fat_control" placeholder="脂肪控制量必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 肌肉控制量">
            <el-input v-model="formInline.Muscle_control" placeholder="肌肉控制量必填数字"></el-input>
          </el-form-item>
        </div>
        <div>
          <el-form-item label="* 内脏指数">
            <el-input v-model="formInline.VFI" placeholder="内脏指数必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 内脏指数最小值">
            <el-input v-model="formInline.VFIMin" placeholder="内脏指数最小值必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 内脏指数最大值">
            <el-input v-model="formInline.VFIMax" placeholder="内脏指数最大值必填数字"></el-input>
          </el-form-item>
        </div>
        <div>
          <el-form-item label="* 水中系数">
            <el-input v-model="formInline.Edema" placeholder="水中系数必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 水中系数最小值">
            <el-input v-model="formInline.EdemaMin" placeholder="水中系数最小值必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 水中系数最大值">
            <el-input v-model="formInline.EdemaMax" placeholder="水中系数最大值必填数字"></el-input>
          </el-form-item>
        </div>
        <div>
          <el-form-item label="* 细胞外液">
            <el-input v-model="formInline.ECW" placeholder="细胞外液必填数字"></el-input>
          </el-form-item>
          <el-form-item label="* 基础代谢">
            <el-input v-model="formInline.BMR" placeholder="基础代谢必填数字"></el-input>
          </el-form-item>
        </div>


        <div style="flex: auto;text-align: center">
          <el-button @click="cancelClick">取消</el-button>
          <el-button type="primary" @click="confirmClick">确定</el-button>
        </div>
      </el-form>
    </template>
    <template #footer>

    </template>
  </el-drawer>
</template>

<script setup>
import { Edit, CirclePlus, Search, Expand, } from '@element-plus/icons-vue'
import { defineComponent, onMounted, ref, watch, reactive, Vue } from 'vue'
import Header from './common/Header.vue';
import ChildLeft from './common/ChildLeft.vue';
import { useRoute, useRouter } from "vue-router"
import http, { baseUrl } from "../utils/http";
import Drawer from './common/Drawer.vue';
import { ElMessage, ElMessageBox } from "element-plus";
import * as api from "../api/loginHttp";
const route = useRoute()
const router = useRouter()
const labName = ref()
const options = ref([
  {
    value: '早餐',
    label: '早餐',
  },
  {
    value: '早加餐',
    label: '早加餐',
  },
  {
    value: '午餐',
    label: '午餐',
  },
  {
    value: '午加餐',
    label: '午加餐',
  },
  {
    value: '晚餐',
    label: '晚餐',
  },
  {
    value: '晚加餐',
    label: '晚加餐',
  }
])
const options1 = ref([{ value: '全部', label: '全部', id: '' }])
const options2 = ref([{ value: '全部', label: '全部', id: '' }])
const options3 = ref([{ value: '全部', label: '全部', id: '' }])
const options4 = ref([{ value: '全部', label: '全部', id: '' }])
const options5 = ref([{ value: '全部', label: '全部', id: '' }])
const checkList = ref([])
const tan = ref(true)
const loadings = ref(true)
const list = ref([])
const energy = ref(0)
const energys = ref(0)
const OilEnergy = ref(0)
const drawer = ref(false)
const inputs = ref()
const value = ref()
const value1 = ref()
const value2 = ref()
const value3 = ref()
const value4 = ref()
const value5 = ref()
const value1_1 = ref()
const value2_1 = ref()
const value3_1 = ref()
const value4_1 = ref()
const pageTotal = ref(0)
const value5_1 = ref()
const page = ref(1)
const foodname = ref()
const limit = ref(8)
const default_1 = ref(false)
const pushstatus=ref()
const saveArry=ref([])

const groupId = ref()
const type_3 = ref()
const tableData = ref()
const id = ref()
const showPopup = ref(false)
const reportId = ref()
const popWrite = ref(false)
const popWriteDirection = ref('ttb')
const pageList = async () => {
  await router.push({ 'path': '/pageList/' + groupId.value })
}
const size = ref('150%')

const formInline = ref({
  Fat: '',
  FatMin: '',
  FatMax: '',
  Bone: '',
  BoneMin: '',
  BoneMax: '',
  Protein: '',
  ProteinMin: '',
  ProteinMax: '',
  Water: '',
  WaterMin: '',
  WaterMax: '',
  LBM: '',
  ICW: '',
  Weight: '',
  WeightMin: '',
  WeightMax: '',
  Muscle: '',
  MuscleMin: '',
  MuscleMax: '',
  PBF: '',
  PBFMin: '',
  PBFMax: '',
  SMM: '',
  SMMMin: '',
  SMMMax: '',
  BMI: '',
  BMIMin: '',
  BMIMax: '',
  WHR: '',
  WHRMin: '',
  WHRMax: '',
  Weight_control: '',
  Fat_control: '',
  Muscle_control: '',
  VFI: '',
  VFIMin: '',
  VFIMax: '',
  Edema: '',
  EdemaMin: '',
  EdemaMax: '',
  ECW: '',
  BMR: '',
})

const onHome = async () => {
  await router.push({ 'path': '/Home' })
}
const previousPage = async (newPage) => {
  if (value1_1.value == undefined) {

    value1_1.value = '';
  }
  if (value2_1.value == undefined) {
    value2_1.value = '';
  }
  if (value3_1.value == undefined) {
    value3_1.value = '';
  }
  if (value4_1.value == undefined) {
    value4_1.value = '';
  }
  if (value5_1.value == undefined) {
    value5_1.value = '';
  }
  http.get('api/Getfood/getFoods?food_product_id=' + value1_1.value + '&food_type=1,5' + '&meals_id=' + value2_1.value + '&age_id=' + value3_1.value + '&divide_id=' + value4_1.value + '&diseases_id=' + value5_1.value + '&page=' + newPage + '&limit=' + limit.value + '&food_name=' + foodname.value).then((res) => {
    tableData.value = res.data.data.list;
    pageTotal.value = res.data.data.count
  })
}
const nextPage = async (newPage) => {
  if (value1_1.value == undefined) {

    value1_1.value = '';
  }
  if (value2_1.value == undefined) {
    value2_1.value = '';
  }
  if (value3_1.value == undefined) {
    value3_1.value = '';
  }
  if (value4_1.value == undefined) {
    value4_1.value = '';
  }
  if (value5_1.value == undefined) {
    value5_1.value = '';
  }
  http.get('api/Getfood/getFoods?food_product_id=' + value1_1.value + '&food_type=1,5' + '&meals_id=' + value2_1.value + '&age_id=' + value3_1.value + '&divide_id=' + value4_1.value + '&diseases_id=' + value5_1.value + '&page=' + newPage + '&limit=' + limit.value + '&food_name=' + foodname.value).then((res) => {
    tableData.value = res.data.data.list;
    pageTotal.value = res.data.data.count
  })
}
//删除
const deletes = (index) => {
  list.value.splice(index, 1)
  energys.value = list.value.reduce((total, obj) => total + obj.energy * 1, 0);
}
//关闭
const closes = () => {
  default_1.value = false;
}

const searss = () => {
  if (value1_1.value == undefined) {

    value1_1.value = '';
  }
  if (value2_1.value == undefined) {
    value2_1.value = '';
  }
  if (value3_1.value == undefined) {
    value3_1.value = '';
  }
  if (value4_1.value == undefined) {
    value4_1.value = '';
  }
  if (value5_1.value == undefined) {
    value5_1.value = '';
  }
  http.get('api/Getfood/getFoods?food_product_id=' + value1_1.value + '&food_type=1,5' + '&meals_id=' + value2_1.value + '&age_id=' + value3_1.value + '&divide_id=' + value4_1.value + '&diseases_id=' + value5_1.value + '&page=' + page.value + '&limit=' + limit.value + '&food_name=' + foodname.value).then((res) => {
    tableData.value = res.data.data.list;
    pageTotal.value = res.data.data.count
  })

}

const handleSizeChange = (page) => {
}
const handleCurrentChange = (page) => {
  if (value1_1.value == undefined) {

    value1_1.value = '';
  }
  if (value2_1.value == undefined) {
    value2_1.value = '';
  }
  if (value3_1.value == undefined) {
    value3_1.value = '';
  }
  if (value4_1.value == undefined) {
    value4_1.value = '';
  }
  if (value5_1.value == undefined) {
    value5_1.value = '';
  }
  http.get('api/Getfood/getFoods?food_product_id=' + value1_1.value + '&food_type=1,5' + '&meals_id=' + value2_1.value + '&age_id=' + value3_1.value + '&divide_id=' + value4_1.value + '&diseases_id=' + value5_1.value + '&page=' + page + '&limit=' + limit.value + '&food_name=' + foodname.value).then((res) => {
    tableData.value = res.data.data.list;
    pageTotal.value = res.data.data.count
  })
}

const adds = () => {
  default_1.value = !default_1.value;
  // food_product_id='';
  // meals_id='';
  // age_id='';
  // divide_id='';
  // diseases_id='';
}
// 开启推送
const opens1=()=>{
  if(pushstatus.value == 1){
      http.get('api/user/setPushStatus?pushStatus='+2).then((res) => {
        if(res.data.code==1){
      pushstatus.value=2;
    }
    })
  }else{
    http.get('api/user/setPushStatus?pushStatus='+1).then((res) => {
      if(res.data.code==1){
        pushstatus.value=1;
      }
  })
  }
}

const option1 = (item) => {
  value1_1.value = item.id
}
const option2 = (item) => {
  value2_1.value = item.id
}
const option3 = (item) => {
  value3_1.value = item.id
}
const option4 = (item) => {
  value4_1.value = item.id
}
const option5 = (item) => {
  value5_1.value = item.id
}

const onChange = async (index, value) => {
  list._rawValue[index].energy = (list._rawValue[index].single.energy * value).toFixed(2)
  list._rawValue[index].weight = (list._rawValue[index].single.weight * value).toFixed(2)
  list._rawValue[index].protein = (list._rawValue[index].single.protein * value).toFixed(2)
  list._rawValue[index].fat = (list._rawValue[index].single.fat * value).toFixed(2)
  list._rawValue[index].carbohydrate = (list._rawValue[index].single.carbohydrate * value).toFixed(2)
  list._rawValue[index].calcium = (list._rawValue[index].single.calcium * value).toFixed(2)
  list._rawValue[index].sodium = (list._rawValue[index].single.sodium * value).toFixed(2)
  list._rawValue[index].magnesium = (list._rawValue[index].single.magnesium * value).toFixed(2)
  list._rawValue[index].iron = (list._rawValue[index].single.iron * value).toFixed(2)
  list._rawValue[index].zinc = (list._rawValue[index].single.zinc * value).toFixed(2)
  list._rawValue[index].selenium = (list._rawValue[index].single.selenium * value).toFixed(2)
  list._rawValue[index].vitamin_a = (list._rawValue[index].single.vitamin_a * value).toFixed(2)
  list._rawValue[index].vitamin_e = (list._rawValue[index].single.vitamin_e * value).toFixed(2)
  list._rawValue[index].vitamin_b1 = (list._rawValue[index].single.vitamin_b1 * value).toFixed(2)
  list._rawValue[index].vitamin_b2 = (list._rawValue[index].single.vitamin_b2 * value).toFixed(2)
  list._rawValue[index].vitamin_c = (list._rawValue[index].single.vitamin_c * value).toFixed(2)
  energys.value = 0;
  for (let i = 0; i < list._rawValue.length; i++) {
    energys.value += (list._rawValue[i].energy) * 1;

  }
  energys.value = Math.floor(energys.value * 100) / 100;
  saveArry.value=list.value;
  //拿到的数据存起来
  console.log(saveArry.value,'改变份数后的数据')
  localStorage.setItem('SaveArry',JSON.stringify(saveArry.value))
}
const handleEdit = async (index, row) => {
  row.single = {};
  row.num = 1;
  row.single.energy = row.energy;
  row.single.weight = row.weight;
  row.single.protein = row.protein;
  row.single.fat = row.fat;
  row.single.carbohydrate = row.carbohydrate;
  row.single.calcium = row.calcium;
  row.single.sodium = row.sodium;
  row.single.magnesium = row.magnesium;
  row.single.iron = row.iron;
  row.single.zinc = row.zinc;
  row.single.selenium = row.selenium;
  row.single.vitamin_a = row.vitamin_a;
  row.single.vitamin_e = row.vitamin_e;
  row.single.vitamin_b1 = row.vitamin_b1;
  row.single.vitamin_b2 = row.vitamin_b2;
  row.single.vitamin_c = row.vitamin_c;
  row.single.checkList = '早餐';
  http.get('api/Getfood/getLateFoods?id=' + row.id + '&memberType=1').then((res) => {
    row.single.replace = res.data.data
  })
  list.value.push(row)
  energys.value = list.value.reduce((total, obj) => total + obj.energy * 1, 0);

}

const replaces = async (value, index) => {
  value.single = {};
  value.single.energy = value.energy;
  value.single.weight = value.weight;
  value.single.protein = value.protein;
  value.single.fat = value.fat;
  value.single.carbohydrate = value.carbohydrate;
  value.single.calcium = value.calcium;
  value.single.sodium = value.sodium;
  value.single.magnesium = value.magnesium;
  value.single.iron = value.iron;
  value.single.zinc = value.zinc;
  value.single.selenium = value.selenium;
  value.single.vitamin_a = value.vitamin_a;
  value.single.vitamin_e = value.vitamin_e;
  value.single.vitamin_b1 = value.vitamin_b1;
  value.single.vitamin_b2 = value.vitamin_b2;
  value.single.vitamin_c = value.vitamin_c;
  value.single.checkList = '早餐';
  http.get('api/Getfood/getLateFoods?id=' + value.id + '&memberType=1').then((res) => {
    value.single.replace = res.data.data
  })
  list.value[index] = value;

}

const btn_fenxi = async () => {
  let params = {
    'id': reportId.value,
    'user_id': id.value,
    'diet_energy': energys.value,
    'diet': list._rawValue,
  }


  const data = await api.diet(params);

  if (data.data.code == 0) {
    ElMessage.error(data.data.msg)
    return false;
  } else {
    const { href } = await router.resolve({
      path: "/Dietary/" + groupId.value + "/" + id.value + '/' + reportId.value
    });
    window.open(href, "_blank");
  }

}

const cancelClick = async () => {
  popWrite.value = false;
}

const confirmClick = async () => {
  Object.keys(formInline._rawValue).forEach(function (item, index) {
    //item 就是当日按循环到的对象
    //index是循环的索引，从0开始
    if (formInline._rawValue[item] == '') {
      ElMessage.error('必填项不能为空')
      return;
    }
  })



  // for(let i = 0; i < Object.keys(formInline._rawValue).length; i++) {
  //   if (formInline.value[i] == '') {
  //     ElMessage.error('必填项不能为空')
  //     return false;
  //   }
  // }
}

const get_new = async () => {
  const res = await api.getFood();
}

// const handClick = async ()  => {
//   console.log('手动输入体成分数据');
// }

const handClick = async () => {
  popWrite.value = true;
}



const appraisal = async () => {
  http.get('api/archives/analysis?id=' + id.value + '&type=analysis').then((res) => {
    if (res.data.code === 0) {
      ElMessage.error(res.data.msg)
    }
  })
}

const guanbi = async () => {
  tan.value = false;
  http.get('api/archives/analysis?id=' + id.value + '&type=cancel').then((res) => {
    if (res.data.code === 0) {
      ElMessage.error(res.data.msg)
    }
  })
}

const goto = async () => {
  const { href } = await router.resolve({
    path: "/analysis/" + groupId.value + "/" + id.value + "/" + reportId.value
  });
  window.open(href, "_blank");
}


const btn = async () => {
  // // 只要点击文档，那么就将数据保存起来
  http.get('api/archives/setReportStatus?reportId=' + reportId.value).then((res) => {
    if (res.data.code === 0) {
      ElMessage.error(res.data.msg)
      return false
    }
  })
  const { href } = await router.resolve({
    path: "/reportView/" + groupId.value + "/" + id.value + "/" + reportId.value
  });
  window.open(href, "_blank");
}

const fetchData = async () => {
  if (route.params.groupId) {
    groupId.value = route.params.groupId
    id.value = route.params.id
    reportId.value = route.params.reportId
    if (groupId.value == 3) {
      labName.value = '婴幼儿营养测评'
    } else if (groupId.value == 5) {
      labName.value = '孕产妇营养测评'
    } else if (groupId.value == 6) {
      labName.value = '成年人营养测评'
    } else if (groupId.value == 7) {
      labName.value = '慢性病营养测评'
    } else if (groupId.value == 8) {
      labName.value = '团体营养测评'
    }
    //获取推送是否开启
    http.get('api/user/getPushStatus').then((res) => {
      pushstatus.value=res.data.data;
    })

    //获取分类
    http.get('api/Getfood/getFoodProduct').then((res) => {
      if (res.data.data.length > 0) {
        for (let i = 0; i < res.data.data.length; i++) {
          options1.value = options1.value.concat({ value: res.data.data[i].title, label: res.data.data[i].title, id: res.data.data[i].id })
        }
      }

    })
    //获取餐品类
    http.get('api/Getfood/getFoodLabel?id=7').then((res) => {
      if (res.data.data.length > 0) {
        for (let i = 0; i < res.data.data.length; i++) {
          options2.value = options2.value.concat({ value: res.data.data[i].label_name, label: res.data.data[i].label_name, id: res.data.data[i].id })
        }
      }
    })
    //获取年龄类
    http.get('api/Getfood/getFoodLabel?id=6').then((res) => {
      if (res.data.data.length > 0) {
        for (let i = 0; i < res.data.data.length; i++) {
          options3.value = options3.value.concat({ value: res.data.data[i].label_describe, label: res.data.data[i].label_describe, id: res.data.data[i].id })
        }
      }
    })
    //获取分餐类
    http.get('api/Getfood/getFoodLabel?id=1').then((res) => {
      if (res.data.data.length > 0) {
        for (let i = 0; i < res.data.data.length; i++) {
          options4.value = options4.value.concat({ value: res.data.data[i].label_name, label: res.data.data[i].label_name, id: res.data.data[i].id })
        }
      }
    })
    //获取疾病类
    http.get('api/Getfood/getFoodLabel?id=2').then((res) => {
      if (res.data.data.length > 0) {
        for (let i = 0; i < res.data.data.length; i++) {
          options5.value = options5.value.concat({ value: res.data.data[i].label_name, label: res.data.data[i].label_name, id: res.data.data[i].id })
        }
      }
    })

    //获取权限
    http.get('api/index/getAuth').then((res) => {
      let aray = res.data.data;
      let array = aray.split(',');
      if (array.includes('13')) {
        type_3.value = 'true';
      } else {
        type_3.value = 'false';
      }

    })

    let ws = new WebSocket('ws://' + baseUrl + ':1234');
    ws.onopen = function () {
      let uid = window.localStorage.getItem("MemberId");
      ws.send(uid);
    };
    ws.onmessage = function (e) {
      loadings.value = true;
      energy.value = 0;
      let WebList = JSON.parse(e.data);
      for (let i = 0; i < WebList.list.length; i++) {
        WebList.list[i].single = {
          energy: WebList.list[i].energy,
          weight: WebList.list[i].weight,
          protein: WebList.list[i].protein,
          fat: WebList.list[i].fat,
          carbohydrate: WebList.list[i].carbohydrate,
          calcium: WebList.list[i].calcium,
          sodium: WebList.list[i].sodium,
          magnesium: WebList.list[i].magnesium,
          iron: WebList.list[i].iron,
          zinc: WebList.list[i].zinc,
          selenium: WebList.list[i].selenium,
          vitamin_a: WebList.list[i].vitamin_a,
          vitamin_e: WebList.list[i].vitamin_e,
          vitamin_b1: WebList.list[i].vitamin_b1,
          vitamin_b2: WebList.list[i].vitamin_b2,
          vitamin_c: WebList.list[i].vitamin_c,
          checkList: '早餐',
          replace: []

        }
        energy.value = energy.value + Number(WebList.list[i].energy);
        energys.value = (energy.value).toFixed(2);

      }
      list.value = WebList.list;
      //先获取本地数据有的话比较没有的话存当前的数据
      saveArry.value=JSON.parse(localStorage.getItem('SaveArry'));
      console.log(saveArry.value,'本地数据')
      if( saveArry.value){
        //筛选不同id的数据
        let result =[];
        for (let i = 0; i < WebList.list.length; i++) {
          let found = false;
          for (let j = 0; j < JSON.parse(localStorage.getItem('SaveArry')).length; j++) {
            if ( WebList.list[i].id == JSON.parse(localStorage.getItem('SaveArry'))[j].id) {
              found = true;
              break;
            }
          }
          if (!found) {
            result.push(WebList.list[i]);
          }
        }
        console.log(result,'不一样的菜')
        //添加到之前的数据中
        JSON.parse(localStorage.getItem('SaveArry')).push(result)
        console.log( JSON.parse(localStorage.getItem('SaveArry')),'添加后的数据')
      }else{
        localStorage.setItem('SaveArry',JSON.stringify(WebList.list))
      }


      // let lowList = saveArry.value;
      // if(lowList) {
      //   lowList.forEach(item => {
      //     WebList.list.forEach(item2 => {
      //       // 等于就删除
      //       if(item2.id == item.id) {
      //         // 这个地方，删除weblist里面id相同的
      //         //然后给 list跟savearry追加，就是push。
      //         // 每次进来，要给list跟savearry坐数据清理
      //       }
      //     });
      //   })
      // } else {
      //   list.value = WebList.list;//这里不能注释
      //   saveArry.value=WebList.list;//第一次进来没数据等于第一次读取的数据
      // }


      //  let lowList = saveArry.value;
      //   console.log(lowList)
      //   if(lowList) {
      //     lowList.forEach(item => {
      //       console.log(item.id, 'aa')
      //       if(WebList.list[i] != item.id) {}
      //     })
      //   }


      //  saveArry.value=WebList.list;
      //  console.log(list.value, 'aaa');
      //  list.value.forEach(item => {
      //     console.log(item.id, bbbbb)
      //   });
     
      for (let i = 0; i < list.value.length; i++) {
        http.get('api/Getfood/getLateFoods?id=' + list.value[i].id + '&memberType=1').then((res) => {
          list._rawValue[i].single.replace = res.data.data
        })
      }
     
      
 
     

      setTimeout(() => {
        loadings.value = false
      }, 1000);
    };

  } else {
    ElMessage.error('栏目编号不得为空')
    await router.push({ 'path': '/Home' })
  }

}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
@import '../assets/css/common.css';
@media screen and (max-width:1920px) {
  .right-box-card .all button {
    width: 85px;
    margin-right: 14px;
  }

  .el-drawer__body .drVal {
    padding-top: 50px;
  }
}

.example-pagination-block+.example-pagination-block {
  margin-top: 10px;
}

.example-pagination-block .example-demonstration {
  margin-bottom: 16px;
}

.types1 {
  display: none;
}

.types2 {
  display: block;
}

.BMIprimary {
  width: 268px;
  height: 70px;
  background: #2AB28B;
  opacity: 1;
  border-radius: 8px;
  font-size: 24px;
}

.BMIDefault {
  width: 268px;
  height: 70px;
  background: #fff;
  opacity: 1;
  color: #2AB28B;
  border-radius: 8px;
  font-size: 24px;
  border: 1px solid #2ab28b;
}

.el-drawer__body {
  overflow: auto;
}

.el-drawer__container ::-webkit-scrollbar {
  display: none;
}

::v-deep .el-button:focus,
.el-button:hover {
  background: #2AB28B;
  color: #fff;
}

::v-deep .el-progress-bar__inner {
  background: #2AB28B;
}
</style>
<style>
.right-box-card {
  margin-left: 2%;
  margin-top: 1%;
  position: static;
  top: calc(260px);
  width: 65%;
  float: left;

}

.right-box-card .all span {
  margin-right: 5px;
  font-size: 14px;
}

.right-box-card .all .num1 {
  width: 100px;
  border: none;
  text-align: center;
  font-size: 20px;
  font-weight: 700;
  color: #3ea4aa;
}

.num {
  width: 62px;
  border: none;
  text-align: center;
  background: #fff;
}

.right-box-card .all button {
  display: inline-block;
  width: 96px;
  height: 40px;
  text-align: center;
  line-height: 40px;
  background: #3ea4aa;
  border-radius: 3px;
  color: #ffffff;
  font-size: 16px;
  float: right;
  margin-right: 40px;
  margin-top: 70px;
  border: none;
}
</style>
